/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  VirtAcctSingleTransDetailQueryService
 *
 *  Edit History:
 *      lizhangtao Created on 2020年05月13日
 *
 */
package cc.rengu.igas.acct.core.service.trans;

import cc.rengu.igas.acct.common.dao.AcctDetailMapper;
import cc.rengu.igas.acct.common.dao.impl.AcctDetailMapperImpl;
import cc.rengu.igas.acct.common.entity.AcctDetail;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.igas.acct.facade.bean.AcctDetailInfo;
import cc.rengu.igas.acct.facade.request.VirtAcctSingleTransDetailQueryRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctSingleTransDetailQueryResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 单笔记账明细查询
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/13
 */
public class VirtAcctSingleTransDetailQueryService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            VirtAcctSingleTransDetailQueryRequest virtAcctSingleTransDetailQueryRequest = new VirtAcctSingleTransDetailQueryRequest();
            ConvertUtil.convertOutput(virtAcctSingleTransDetailQueryRequest);
            rglog.info("virtAcctSingleTransDetailQueryRequest = <{}>", JSONObject.toJSONString(virtAcctSingleTransDetailQueryRequest));
            VirtAcctSingleTransDetailQueryResponse virtAcctSingleTransDetailQueryResponse = new VirtAcctSingleTransDetailQueryResponse();
            virtAcctSingleTransDetailQueryResponse.setHeader(virtAcctSingleTransDetailQueryRequest.getHeader());
            BizResponse<VirtAcctSingleTransDetailQueryResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(virtAcctSingleTransDetailQueryResponse);
            String instId = virtAcctSingleTransDetailQueryRequest.getHeader().getInstId();
            String acctNo = virtAcctSingleTransDetailQueryRequest.getAcctNo();
            String acctType = virtAcctSingleTransDetailQueryRequest.getAcctType();
            String customerId = virtAcctSingleTransDetailQueryRequest.getCustomerId();
            String userId = virtAcctSingleTransDetailQueryRequest.getUserId();
            String userType = virtAcctSingleTransDetailQueryRequest.getUserType();
            //账户号、客户id，外部用户类型&外部用户id必须三选一
            if (StringUtil.isNullorEmpty(acctNo) &&
                    StringUtil.isNullorEmpty(customerId) &&
                    (StringUtil.isNullorEmpty(userType) || StringUtil.isNullorEmpty(userId))) {
                rglog.error("账户号、客户id，外部用户类型&外部用户id必须三选一");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            //必填项
            if (StringUtil.isNullorEmpty(acctType)) {
                rglog.error("账户类型、交易开始日期，交易截止日期必输");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            //查询交易明细
            AcctDetail acctDetail;
            AcctDetailMapper acctDetailMapper = new AcctDetailMapperImpl();
            if (!StringUtil.isNullorEmpty(virtAcctSingleTransDetailQueryRequest.getAcctSeqNum())) {
                acctDetail = acctDetailMapper.selectAcctDetailByAcctSeqNum(instId,
                        virtAcctSingleTransDetailQueryRequest.getAcctSeqNum());
                if (null == acctDetail) {
                    rglog.error("机构<{}>记账编号<{}>未查询到数据", instId, virtAcctSingleTransDetailQueryRequest.getAcctSeqNum());
                    throw new BizException(AcctRespCodeEnum.NO_TRANS_RECORD.getRespCode(),
                            AcctRespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                }
                //若也上送了前端流水号则需要校验
                if (!StringUtil.isNullorEmpty(virtAcctSingleTransDetailQueryRequest.getTransSeqNum())) {
                    //原记账流水号与原前端上送流水号不为同一个交易
                    if (!virtAcctSingleTransDetailQueryRequest.getTransSeqNum().equals(acctDetail.getFrontSeqNum())) {
                        rglog.error("原记账流水号<{}>与原前端上送流水号不为同一个交易", virtAcctSingleTransDetailQueryRequest.getAcctSeqNum(),
                                virtAcctSingleTransDetailQueryRequest.getTransSeqNum());
                        throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                                AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
                    }
                }
            }
            //不上送原记账流水
            else {
                //也不上送原交易流水
                if (StringUtil.isNullorEmpty(virtAcctSingleTransDetailQueryRequest.getTransSeqNum())) {
                    throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                            AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
                }
                //上送原交易流水，使用交易流水查询
                acctDetail = acctDetailMapper.selectAcctDetailByTransSeqNum(instId,
                        virtAcctSingleTransDetailQueryRequest.getTransSeqNum());
                if (null == acctDetail) {
                    rglog.error("机构<{}>前端流水号<{}>未查询到数据", instId, virtAcctSingleTransDetailQueryRequest.getTransSeqNum());
                    throw new BizException(AcctRespCodeEnum.NO_TRANS_RECORD.getRespCode(),
                            AcctRespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                }
            }

            //校验上送的账号、客户号、用户类型以及用户id
            if (!StringUtil.isNullorEmpty(acctNo) && !acctNo.equals(acctDetail.getAcctNo())) {
                rglog.error("上送的账号错误");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            //校验上送的客户号
            if (!StringUtil.isNullorEmpty(customerId) && !acctDetail.getCustomerId().equals(customerId)) {
                rglog.error("上送的客户号错误");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            //用户类型&用户id不为空校验用户类型&用户id
            if ((!StringUtil.isNullorEmpty(userType) && !StringUtil.isNullorEmpty(userId))) {
                if (!userType.equals(acctDetail.getUserType()) || !userId.equals(acctDetail.getUserId())) {
                    rglog.error("上送的外部用户错误");
                    throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                            AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
                }
            }

            AcctDetailInfo acctDetailInfo = new AcctDetailInfo();
            BeanUtil.beanCopy(acctDetail, acctDetailInfo);
            //只有transSeqNum在AcctDetail中为frontSeqNum
            acctDetailInfo.setTransSeqNum(acctDetail.getFrontSeqNum());
            virtAcctSingleTransDetailQueryResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            virtAcctSingleTransDetailQueryResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            virtAcctSingleTransDetailQueryResponse.setAcctDetailInfo(acctDetailInfo);
            bizResponse.setRspSysId(ChannelEnum.ACCT.getChannelType());
            bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
            bizResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            ConvertUtil.convertInput(bizResponse.getResult());
        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE,
                        OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC,
                        OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
